<template>
  <a-row id="globalHeader" align="center" :wrap="false">
    <a-col flex="auto">
      <a-menu
        mode="horizontal"
        @menu-item-click="doMenuClick"
        :selected-keys="selectedKeys"
      >
        <a-menu-item
          key="0"
          :style="{ padding: 0, marginLeft: '16px', marginRight: '16px' }"
          disabled
        >
          <div class="title-bar">
            <img class="logo" src="../assets/wen-da-logo.svg" alt="" />
            <div class="title">WEN-答</div>
          </div>
        </a-menu-item>
        <a-menu-item
          class="bold-text"
          v-for="item in visibleRoutes"
          :key="item.path"
        >
          {{ item.name }}
        </a-menu-item>
      </a-menu>
    </a-col>
    <a-col flex="100px">
      <div v-if="loginUserStore.loginUser.id">
        {{ loginUserStore.loginUser.userName ?? "匿名用户" }}
      </div>
      <div v-else>
        <a-button type="primary" href="/user/login">登录</a-button>
      </div>
    </a-col>
  </a-row>
</template>

<script setup lang="ts">
import { routes } from "@/router/routes";
import { useRouter } from "vue-router";
import { computed, ref } from "vue";
import { useLoginUserStore } from "@/store/user";
import checkAccess from "@/access/checkAccess";

const loginUserStore = useLoginUserStore();

const router = useRouter();
/**
 * 点击菜单跳转到对应页面
 * @param key
 */
const doMenuClick = (key: string) => {
  router.push({
    path: key,
  });
};

// 当前选中的菜单项
const selectedKeys = ref(["/"]);
/**
 * 路由跳转时，自动更新选中的菜单项
 */
router.afterEach((to) => {
  selectedKeys.value = [to.path];
});

/**
 * 展示在菜单的路由数组
 */
const visibleRoutes = computed(() => {
  return routes.filter((item) => {
    return (
      !item.meta?.hideInMenu &&
      checkAccess(loginUserStore.loginUser, item.meta?.access as string)
    );
  });
});
</script>

<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  margin-left: 4px;
  color: rgb(11, 164, 244);
  font-weight: bold;
  font-size: 24px;
  font-family: "Arial", sans-serif;
  letter-spacing: 2px;
  text-transform: uppercase;
  text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.logo {
  width: 48px;
  height: 48px;
  margin-right: 8px;
  padding: 4px;
}

.bold-text {
  font-weight: bold;
  font-size: 20px;
}
</style>
